Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 7749x 7749x 62160x 912x 912x 912x 984x 984x 288x 984x 912x 912x 61248x 7749x 144x 144x 672x 96x 576x 144x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 33x 33x 33x 33x 3x 3x 3x 3x 3x 3x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 3x 3x 33x 3x 3x 3x 3x 3x 3x | /* eslint-disable @typescript-eslint/no-unused-vars */
import i18n, { type i18n as I18nType } from 'i18next';
import { APP_NAME } from '@/constants/app';
import en from '../locales/en.json';
import es from '../locales/es.json';
import zh from '../locales/zh.json';
import pt from '../locales/pt.json';
import fr from '../locales/fr.json';
import ru from '../locales/ru.json';
import tr from '../locales/tr.json';
import sk from '../locales/sk.json';
// user namespace stays bundled for now
import userEn from '../locales/user/en.json';
import userEs from '../locales/user/es.json';
import userZh from '../locales/user/zh.json';
import userPt from '../locales/user/pt.json';
import userFr from '../locales/user/fr.json';
import userRu from '../locales/user/ru.json';
import userTr from '../locales/user/tr.json';
import userSk from '../locales/user/sk.json';
// extension packs (kept bundled)
import extAdmin from '../locales/ext/admin.ext.json';
/**
* Expand keys that contain dots into nested objects.
* Example: { "deviceManagement.title": "Title" } => { deviceManagement: { title: "Title" } }
* This handles cases where locale JSONs contain flattened keys with dots.
*/
function expandDotKeys(obj: Record<string, any>): Record<string, any> {
const out: Record<string, any> = {};
for (const [key, value] of Object.entries(obj)) {
if (key.includes('.')) {
const parts = key.split('.');
let cursor = out;
for (let i = 0; i < parts.length - 1; i++) {
const p = parts[i];
if (typeof cursor[p] !== 'object' || cursor[p] === null) {
cursor[p] = {};
}
cursor = cursor[p];
}
const last = parts[parts.length - 1];
cursor[last] = (value !== null && typeof value === 'object') ? expandDotKeys(value) : value;
} else {
out[key] = (value !== null && typeof value === 'object') ? expandDotKeys(value) : value;
}
}
return out;
}
/**
* Deep-merge two objects. Properties from `source` are merged into `target`
* recursively so nested objects are combined instead of replaced.
*/
function deepMerge(
target: Record<string, any>,
source: Record<string, any>,
): Record<string, any> {
const out = { ...target };
for (const [key, val] of Object.entries(source)) {
if (
val !== null &&
typeof val === 'object' &&
!Array.isArray(val) &&
out[key] !== null &&
typeof out[key] === 'object' &&
!Array.isArray(out[key])
) {
out[key] = deepMerge(out[key], val);
} else {
out[key] = val;
}
}
return out;
}
/**
* Build core resources (keep runtime bundle small by excluding large feature namespaces).
* We'll register feature namespaces (like 'admin') dynamically using loadNamespace.
*/
const expandedEn = expandDotKeys(en as unknown as Record<string, any>);
const expandedEs = expandDotKeys(es as unknown as Record<string, any>);
const expandedZh = expandDotKeys(zh as unknown as Record<string, any>);
const expandedPt = expandDotKeys(pt as unknown as Record<string, any>);
const expandedFr = expandDotKeys(fr as unknown as Record<string, any>);
const expandedRu = expandDotKeys(ru as unknown as Record<string, any>);
const expandedTr = expandDotKeys(tr as unknown as Record<string, any>);
const expandedSk = expandDotKeys(sk as unknown as Record<string, any>);
const expandedUserEn = expandDotKeys(userEn as unknown as Record<string, any>);
const expandedUserEs = expandDotKeys(userEs as unknown as Record<string, any>);
const expandedUserZh = expandDotKeys(userZh as unknown as Record<string, any>);
const expandedUserPt = expandDotKeys(userPt as unknown as Record<string, any>);
const expandedUserFr = expandDotKeys(userFr as unknown as Record<string, any>);
const expandedUserRu = expandDotKeys(userRu as unknown as Record<string, any>);
const expandedUserTr = expandDotKeys(userTr as unknown as Record<string, any>);
const expandedUserSk = expandDotKeys(userSk as unknown as Record<string, any>);
// Expand extension packs (optional modular additions)
const ext = (extAdmin as unknown as Record<string, any>) || {};
const expandedExtEn = expandDotKeys((ext.en ?? {}) as Record<string, any>);
const expandedExtEs = expandDotKeys((ext.es ?? {}) as Record<string, any>);
const expandedExtZh = expandDotKeys((ext.zh ?? {}) as Record<string, any>);
const expandedExtPt = expandDotKeys((ext.pt ?? {}) as Record<string, any>);
const expandedExtFr = expandDotKeys((ext.fr ?? {}) as Record<string, any>);
const expandedExtRu = expandDotKeys((ext.ru ?? {}) as Record<string, any>);
const expandedExtTr = expandDotKeys((ext.tr ?? {}) as Record<string, any>);
const expandedExtSk = expandDotKeys((ext.sk ?? {}) as Record<string, any>);
/**
* Core resources — intentionally small. Feature namespaces such as "admin"
* will be loaded dynamically when a component requests them.
*/
const resources = {
en: {
translation: {
...deepMerge(expandedEn, expandedExtEn),
user: expandedUserEn.user ?? expandedUserEn
},
user: expandedUserEn,
// Convenience namespaces for code that calls useTranslation('admin'|'reseller'|'common').
admin: deepMerge(expandedEn.admin ?? {}, expandedExtEn.admin ?? {}),
reseller: expandedEn.reseller ?? {},
common: expandedEn.common ?? {}
},
es: {
translation: {
...deepMerge(expandedEs, expandedExtEs),
user: expandedUserEs.user ?? expandedUserEs
},
user: expandedUserEs,
admin: deepMerge(expandedEs.admin ?? {}, expandedExtEs.admin ?? {}),
reseller: expandedEs.reseller ?? {},
common: expandedEs.common ?? {}
},
zh: {
translation: {
...deepMerge(expandedZh, expandedExtZh),
user: expandedUserZh.user ?? expandedUserZh
},
user: expandedUserZh,
admin: deepMerge(expandedZh.admin ?? {}, expandedExtZh.admin ?? {}),
reseller: expandedZh.reseller ?? {},
common: expandedZh.common ?? {}
},
pt: {
translation: {
...deepMerge(expandedPt, expandedExtPt),
user: expandedUserPt.user ?? expandedUserPt
},
user: expandedUserPt,
admin: deepMerge(expandedPt.admin ?? {}, expandedExtPt.admin ?? {}),
reseller: expandedPt.reseller ?? {},
common: expandedPt.common ?? {}
},
fr: {
translation: {
...deepMerge(expandedFr, expandedExtFr),
user: expandedUserFr.user ?? expandedUserFr
},
user: expandedUserFr,
admin: deepMerge(expandedFr.admin ?? {}, expandedExtFr.admin ?? {}),
reseller: expandedFr.reseller ?? {},
common: expandedFr.common ?? {}
},
ru: {
translation: {
...deepMerge(expandedRu, expandedExtRu),
user: expandedUserRu.user ?? expandedUserRu
},
user: expandedUserRu,
admin: deepMerge(expandedRu.admin ?? {}, expandedExtRu.admin ?? {}),
reseller: expandedRu.reseller ?? {},
common: expandedRu.common ?? {}
},
tr: {
translation: {
...deepMerge(expandedTr, expandedExtTr),
user: expandedUserTr.user ?? expandedUserTr
},
user: expandedUserTr,
admin: deepMerge(expandedTr.admin ?? {}, expandedExtTr.admin ?? {}),
reseller: expandedTr.reseller ?? {},
common: expandedTr.common ?? {}
},
sk: {
translation: {
...deepMerge(expandedSk, expandedExtSk),
user: expandedUserSk.user ?? expandedUserSk
},
user: expandedUserSk,
admin: deepMerge(expandedSk.admin ?? {}, expandedExtSk.admin ?? {}),
reseller: expandedSk.reseller ?? {},
common: expandedSk.common ?? {}
}
};
/**
* Simple cache to avoid re-loading the same namespace multiple times.
*/
const loadedNamespaces = new Set<string>();
const SUPPORTED_LANGS = ['en', 'es', 'zh', 'pt', 'fr', 'ru', 'tr', 'sk'] as const;
function normalizeLng(input: string | undefined | null): string {
const code = (input ?? 'en').replace('_', '-');
const base = code.split('-')[0];
const supported = SUPPORTED_LANGS as readonly string[];
Eif (supported.includes(code)) return code;
if (supported.includes(base)) return base;
return 'en';
}
/**
* Dynamically load a namespace for a given language.
* This uses ESM dynamic imports which will be tree-shaken by bundlers and
* create separate chunks for each namespace+language pair.
*
* Usage:
* await loadNamespace('en', 'admin');
*/
/**
* Map legacy dotted keys (admin.<page>.*) to their per-page namespaces (admin/<page>).
* This is a TEMPORARY compatibility layer so existing components using dotted keys
* keep working while we migrate them to useTranslation('admin/<page>') + t('...').
*/
function wrapTWithCompatibility() {
Iif ((i18n as any).__compat_wrapped) return;
const origT = i18n.t.bind(i18n);
function ensureDynamicNamespacesLoaded(lng: string, key: unknown, opts?: any) {
const namespaces = new Set<string>();
if (typeof key === 'string') {
const colonIdx = key.indexOf(':');
if (colonIdx > 0) {
const nsFromKey = key.slice(0, colonIdx);
if (nsFromKey.includes('/')) namespaces.add(nsFromKey);
}
}
const nsOpt = opts?.ns;
if (typeof nsOpt === 'string') {
if (nsOpt.includes('/')) namespaces.add(nsOpt);
} else if (Array.isArray(nsOpt)) {
for (const ns of nsOpt) {
if (typeof ns === 'string' && ns.includes('/')) namespaces.add(ns);
}
}
for (const ns of namespaces) {
loadNamespace(lng, ns).catch(() => { });
}
}
const PAGE_MAP: Record<string, string> = {
// admin pages we split into per-page namespaces
'admin.overview': 'admin/overview',
'admin.activity': 'admin/activityDashboard',
'admin.loginHistory': 'admin/loginHistory',
'admin.securityAlerts': 'admin/securityAlerts',
'admin.masterKeys': 'admin/masterKeys',
'admin.demos': 'admin/demos',
'admin.giftcodes': 'admin/giftcodes',
'admin.billing': 'admin/billing',
'admin.devices': 'admin/devices',
'admin.content': 'admin/content',
'admin.tickets': 'admin/tickets',
'admin.activityLogs': 'admin/activityLogs',
'admin.viewingHistory': 'admin/viewingHistory',
'admin.systemConfiguration': 'admin/systemConfiguration',
'admin.resellerManagement': 'admin/resellerManagement',
'admin.endUserManagement': 'admin/endUserManagement',
'admin.mediaScanner': 'admin/mediaScanner',
'admin.tvCategories': 'admin/tvCategories',
'admin.createContent': 'admin/createContent',
'admin.createSeries': 'admin/createSeries',
'admin.epg': 'admin/epg',
'admin.appUpdates': 'admin/appUpdates',
// security pages (compat for dotted keys)
'admin.securityBans': 'admin/securityBans',
'admin.securityIncidents': 'admin/securityIncidents',
'admin.blacklistedDevices': 'admin/blacklistedDevices',
// additional admin pages
'admin.createBan': 'admin/createBan',
'admin.search': 'admin/search',
'admin.dashboard': 'admin/dashboard',
'admin.provider': 'admin/tvProviders'
};
// Infer dynamic namespace from first key segment for components that
// call t('content.*') without explicitly binding the namespace.
const ROOT_NS_MAP: Record<string, string> = {
content: 'admin/content',
devices: 'admin/devices',
createContent: 'admin/createContent',
editContent: 'admin/editContent',
seriesManagement: 'admin/createSeries',
apiKeys: 'reseller/apiKeys',
episodes: 'admin/episodes',
specializedContent: 'admin/specializedContent',
};
i18n.t = ((key: any, opts?: any) => {
const lng = i18n.language || 'en';
// Ensure per-page namespaces (admin/<page>) load when used via useTranslation('admin/<page>')
// or via explicit colon keys like "admin/<page>:key".
ensureDynamicNamespacesLoaded(lng, key, opts);
// Resolve common root keys (content.*, createContent.*, etc.) against
// their dynamic namespaces to avoid showing raw keys during lazy loading.
if (typeof key === 'string' && !key.includes(':')) {
const root = key.split('.')[0];
const inferredNs = ROOT_NS_MAP[root];
if (inferredNs) {
loadNamespace(lng, inferredNs).catch(() => { });
const namespacedKey = `${inferredNs}:${key}`;
const out = origT(namespacedKey, opts);
if (out !== namespacedKey) return out;
const out2 = origT(key, { ...opts, ns: inferredNs });
if (out2 !== key) return out2;
}
}
// only handle string keys
if (typeof key === 'string' && key.startsWith('admin.')) {
// find which admin.<page>.* it belongs to
const parts = key.split('.');
if (parts.length >= 3) {
const pagePrefix = `${parts[0]}.${parts[1]}`; // e.g., admin.billing
const rest = parts.slice(2).join('.'); // e.g., overview.title
const ns = PAGE_MAP[pagePrefix];
if (ns) {
// ensure the per-page namespace is loaded for current language
// fire and forget; translation may resolve after load, so we also try immediate
loadNamespace(lng, ns).catch(() => { });
// Try resolving using explicit namespace override
const namespacedKey = `${ns}:${rest}`;
const out = origT(namespacedKey, opts);
if (out !== namespacedKey) return out;
// fallback attempt without colon (if already in correct ns)
const out2 = origT(rest, { ...opts, ns });
if (out2 !== rest) return out2;
// compatibility: some per-page bundles keep keys nested under admin.<page>.*
const innerDotted = `${pagePrefix}.${rest}`;
const out3 = origT(innerDotted, { ...opts, ns });
if (out3 !== innerDotted) return out3;
// compatibility: most per-page bundles nest keys under a root object (e.g. { "content": { ... } })
// so allow admin.<page>.<k> to resolve as <page>.<k> (and also ns tail) within the namespace.
const rootCandidates = new Set<string>([
parts[1], // e.g. "content" from admin.content.*
ns.split('/').pop() || '', // e.g. "activityDashboard" from admin/activityDashboard
]);
for (const root of rootCandidates) {
if (!root) continue;
const prefixed = rest.startsWith(`${root}.`) ? rest : `${root}.${rest}`;
const out4 = origT(prefixed, { ...opts, ns });
if (out4 !== prefixed) return out4;
}
}
}
}
return origT(key, opts);
}) as any;
(i18n as any).__compat_wrapped = true;
}
export async function loadNamespace(lng: string, ns: string): Promise<void> {
// Always load EN for dynamic namespaces so fallbackLng can work without showing raw keys.
const normalized = normalizeLng(lng);
await Promise.all([
loadNamespaceInner('en', ns),
normalized === 'en' ? Promise.resolve() : loadNamespaceInner(normalized, ns),
]);
}
async function loadNamespaceInner(lng: string, ns: string): Promise<void> {
const key = `${lng}:${ns}`;
Iif (loadedNamespaces.has(key)) return;
try {
// Try to dynamically import the namespace file under ../locales/{ns}/{lng}.json
// Fallback to a namespaced ext pack if present.
let bundle: Record<string, any> | undefined;
try {
bundle = (await import(`../locales/${ns}/${lng}.json`)).default;
} catch (_e) {
if (ns === 'admin' && (ext[lng]?.admin)) {
bundle = ext[lng].admin;
} else {
bundle = undefined;
}
}
Eif (bundle) {
const expanded = expandDotKeys(bundle as Record<string, any>);
i18n.addResourceBundle(lng, ns, expanded, true, true);
loadedNamespaces.add(key);
}
} catch (_err) {
// ignore
}
}
const CRITICAL_DYNAMIC_NAMESPACES = [
'admin/content',
'admin/devices',
'admin/createContent',
'admin/editContent',
'admin/editTVChannel',
'admin/editEvent',
'admin/editSeries',
'admin/createSeries',
'admin/episodes',
'admin/specializedContent',
'reseller/apiKeys',
];
function preloadCriticalNamespacesForLanguage(lng: string) {
for (const ns of CRITICAL_DYNAMIC_NAMESPACES) {
loadNamespace(lng, ns).catch(() => { });
}
}
Eif (!i18n.isInitialized) {
// Always start with 'en' to avoid SSR/CSR hydration mismatch.
// The actual user language from localStorage is synced in providers.tsx after hydration.
i18n.init({
resources,
lng: 'en',
fallbackLng: 'en',
supportedLngs: SUPPORTED_LANGS as unknown as string[],
nonExplicitSupportedLngs: true,
// include 'admin' in the list of potential namespaces so useTranslation('admin') will work
ns: ['translation', 'user', 'admin', 'reseller', 'common'],
defaultNS: 'translation',
fallbackNS: ['translation', 'user', 'common'],
interpolation: {
escapeValue: false,
defaultVariables: {
appName: APP_NAME
}
},
react: {
useSuspense: false,
nsMode: 'fallback',
bindI18n: 'languageChanged loaded',
bindI18nStore: 'added removed'
}
});
// Enable legacy dotted admin.* compatibility shim (maps to admin/<page>)
wrapTWithCompatibility();
// Preload frequently-used dynamic namespaces to reduce key-flash
// when opening admin content pages and dialogs.
preloadCriticalNamespacesForLanguage(i18n.language || 'en');
i18n.on('languageChanged', preloadCriticalNamespacesForLanguage);
}
export default i18n;
|